package com.liujit.bms.pc.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.liujit.bms.pc.domain.query.BlogArticleQuery;
import com.liujit.bms.pc.domain.result.BlogArchiveResult;
import com.liujit.bms.pc.domain.result.BlogArticleResult;
import com.liujit.bms.pc.service.BlogArticleService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.utils.DozerConvertor;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 文章归档
 * @Author: liujun
 * @Date: 2021/7/3 2:41 下午
 */
@RequiredArgsConstructor
@Controller
@RequestMapping("/blog/archive")
public class ArchiveController {

    private final BlogArticleService blogArticleService;

    @ApiOperation(value = "文章归档")
    @GetMapping
    public String archive(Model model, Integer page) {
        BlogArticleQuery query = new BlogArticleQuery();
        query.setPageNumber(page);
        query.setSortName(ColumnConstant.UPDATE_TIME);
        query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        List<BlogArchiveResult> articleList = new ArrayList<>();
        PageBean<BlogArticleResult> pageBean = blogArticleService.getPage(query);
        List<BlogArticleResult> blogArticleList = pageBean.getRows();
        for (BlogArticleResult blogArticle : blogArticleList) {
            BlogArchiveResult blogArchive = DozerConvertor.convertor(blogArticle, BlogArchiveResult.class);
            blogArchive.setYm(DateUtil.format(blogArchive.getUpdateTime(), "yyyy-MM"));
            blogArchive.setD(DateUtil.format(blogArchive.getUpdateTime(), "dd"));
            articleList.add(blogArchive);
        }
        model.addAttribute("total", pageBean.getTotal());
        model.addAttribute("page", pageBean.getPageNumber());
        model.addAttribute("pages", pageBean.getPages());
        model.addAttribute("articleList", articleList);
        return "archive";
    }
}
